﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HostAgentCore
{
    public abstract class HostAgentBase : IHostAgentBase
    {
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing)
        {
            // nothing to do...
        }

        public event EventHandler<SimpleMessageEventArgs> Trace;

        public abstract bool TryConnect();
        public abstract void ReleaseDeviceConnection();
        public abstract InstallationResult Install(ApplicationDefinition applicationDefinition);
        public abstract UninstallationResult Uninstall(ApplicationDefinition applicationDefinition);
        public abstract StopResult Stop(ApplicationDefinition applicationDefinition);
        public abstract StartResult Start(ApplicationDefinition applicationDefinition);

        public InstallationResult ForceInstall(ApplicationDefinition applicationDefinition)
        {
            ForceUninstall(applicationDefinition);
            return Install(applicationDefinition);
        }

        public UninstallationResult ForceUninstall(ApplicationDefinition applicationDefinition)
        {
            Stop(applicationDefinition);
            return Uninstall(applicationDefinition);
        }

        public StartResult ForceStart(ApplicationDefinition applicationDefinition)
        {
            Stop(applicationDefinition);
            return Start(applicationDefinition);
        }

        protected void InvokeTrace(string message, params object[] args)
        {
            SimpleMessageEventArgs e = new SimpleMessageEventArgs() { Message = string.Format(message, args) };
            EventHandler<SimpleMessageEventArgs> handler = Trace;
            if (handler != null) handler(this, e);
        }
    }
}
